<template>
  <div class="login-container">
    <div class="login-card">
      <h1 class="login-title">OnCallMaster</h1>
      <p class="login-subtitle">值班管理平台</p>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        <el-button
          type="primary"
          :loading="loading"
          class="login-button"
          @click="handleLogin"
        >
          登录
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useUserStore } from '@/store/user';
import type { FormInstance, FormRules } from 'element-plus';

// 路由实例
const router = useRouter();
const route = useRoute();

// 用户Store
const userStore = useUserStore();

// 表单引用
const loginFormRef = ref<FormInstance>();

// 加载状态
const loading = ref(false);

// 登录表单
const loginForm = reactive({
  username: '',
  password: '',
});

// 表单验证规则
const loginRules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
});

// 登录处理
const handleLogin = async () => {
  if (!loginFormRef.value) return;

  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true;
        const result = await userStore.login(loginForm);
        if (result) {
          await userStore.getInfo();
          ElMessage.success('登录成功');
          
          // 获取重定向地址
          const redirect = route.query.redirect as string;
          router.push(redirect || '/');
        }
      } catch (error) {
        console.error('登录失败:', error);
      } finally {
        loading.value = false;
      }
    }
  });
};
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
  
  .login-card {
    width: 360px;
    padding: 30px;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    text-align: center;
    
    .login-title {
      font-size: 24px;
      margin-bottom: 8px;
    }
    
    .login-subtitle {
      font-size: 14px;
      color: #909399;
      margin-bottom: 20px;
    }
    
    .login-form {
      text-align: left;
    }
    
    .login-button {
      width: 100%;
      margin-top: 10px;
    }
  }
}
</style> 